import request from "../util/request.js";
// import { loginUtil } from "../Service/login.js";

import { getUserId } from "@/Service/huo-qu.js";

export default {
    //保存设置
    async saveSettings() {
        const formData = new FormData();

        // 构建 systemEntity 对象，**不包含 Base64 图像**
        const systemEntity = {
            id: parseInt(getUserId()),
            backgroundColor: this.currentUser.backgroundColor,
            avatarUrl: this.currentUser.avatarUrl || "default.png",
        };

        // 添加 JSON 格式的 systemEntity
        formData.append(
            "systemEntity",
            new Blob([JSON.stringify(systemEntity)], {
                type: "application/json",
            })
        );

        // 添加头像文件（如果存在）
        if (this.userAvatarFile) {
            formData.append("avatar", this.userAvatarFile);
        }

        try {
            const response = await request.put(
                "/system/${id}/userExpansionInformation",
                formData, {
                    headers: {
                        "Content-Type": "multipart/form-data",
                    },
                }
            );

            if (response.data.code === 200) {
                // alert("保存成功");
            } else {
                alert("保存失败: " + response.data.message);
            }
        } catch (error) {
            console.error("请求失败:", error);
            alert("网络错误，请重试");
        }
    },
};